package cn.dt70.controller.login;

import cn.dt70.dao.user.UserMapper;
import cn.dt70.services.UserService;
import cn.dt70.utils.Constrants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login/")
public class Login {
    @Resource
    private UserService userService;

    @RequestMapping("houtai")
    public String houtai() {
        return "backendlogin";
    }


    //    跳转到开发者登录页面
    @RequestMapping("qiantai")
    public String qiantai() {
        return "devlogin";
    }

    //    开发者登录
    @RequestMapping("devLogin")
    public String devLogin(@RequestParam("devCode") String devCode,
                           @RequestParam("devPassword") String devPassword,
                           HttpSession session, Model model) {
        if (userService.dologin( devCode, devPassword )) {
            //登录陈成功，把用户存入session:
            session.setAttribute( Constrants.USER_SESSION, userService.qryUserByCode( devCode ) );
            //跳转主页
            return "developer/main";
        } else {
//                登录失败
            model.addAttribute( "retCode", "F" );
            model.addAttribute( "retMsg", "用户名或密码不正确" );
            return "devlogin";
        }
    }

    //    开发者注销
    @RequestMapping("logout")
    public String logout(HttpSession session,Model model) {
        session.removeAttribute( Constrants.USER_SESSION );
        model.addAttribute( "reCode","F" );
        model.addAttribute( "retMsg","账号已失效，请重新登录" );
        return "devlogin";
    }


}
